﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Web;
using FaceSharp.Api.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;
using RestSharp;

namespace FaceSharp.Api.RestApi
{


    public class RestBase
    {
        private readonly IFacebookCore _facebookCore;
        public RestBase(IFacebookCore facebookCore)
        {
            _facebookCore = facebookCore;
        }


        public enum ResponseFormat
        {
            Json,
            Xml
        }

        public string ExecuteFql(string query,ResponseFormat responseFormat = ResponseFormat.Json, bool pretty = false)
        {
            Trace.Write("Begin Request", "RestApi.ExecuteFql");
            var results = BaseRestRequest("fql.query",
                new Dictionary<string, string>()
                    {
                        {"query", query}
                    },
                responseFormat:responseFormat,
                pretty:pretty
                );
            Trace.Write("End Request", "RestApi.ExecuteFql");
            return results;
        }

        public string ExecuteFqlMultiQuery(IOrderedDictionary queries, ResponseFormat responseFormat = ResponseFormat.Json, bool pretty = false)
        {
            Trace.Write("Begin Request", "RestApi.ExecuteFql");
            var jsonQueries = JsonConvert.SerializeObject(queries);
            var results = BaseRestRequest("fql.multiquery",
                new Dictionary<string, string>()
                    {
                        {"queries", jsonQueries}
                    },
                responseFormat: responseFormat,
                pretty: pretty
                );
            Trace.Write("End Request", "RestApi.ExecuteFql");

            return results;
        }

        public string MakeRestRequest(string method, IDictionary<string, string> parameters = null, bool isPost = false, bool signRequest = false, ResponseFormat responseFormat = ResponseFormat.Json, bool pretty = false)
        {
            var results = BaseRestRequest(method, parameters, isPost, signRequest,responseFormat,pretty);

            return results;
        }

        public string BaseRestRequest(string method, IDictionary<string, string> parameters, bool isPost = false, bool signRequest = false, ResponseFormat responseFormat = ResponseFormat.Json, bool pretty = false)
        {
            string results = null;
            var client = new RestSharp.RestClient("https://api.facebook.com/");
            var request = new RestSharp.RestRequest("restserver.php",isPost ? Method.POST : Method.GET);

            if (signRequest)
            {
                var sigList = (from parameter in parameters
                               orderby parameter.Key
                               select parameter.Key + "=" + parameter.Value).Aggregate((x, y) => x + y);
                var md5 = (sigList + _facebookCore.ApplicationSecret).MD5Encrypt().ToLower();
                parameters.Add("sig", md5);
            }

            if (parameters != null) parameters.ForEach(x => request.AddParameter(x.Key, x.Value));

            request.AddParameter("access_token", _facebookCore.AccessToken);
            request.AddParameter("method", method);
            request.AddParameter("format",responseFormat.ToString().ToLower());
            request.AddParameter("pretty",pretty ? "1" : "0");
            

            var response = client.Execute(request);
            if (response.StatusCode == HttpStatusCode.OK)
            {
                results = response.Content;
            }
            else
            {
                Trace.TraceWarning("Status Code: " + response.StatusCode + " Content: " + response.Content + " Headers : " + response.Headers);
            }
            return results;
        }
        
    }
}